//
//  InitializationEmailSender.m
//  JigSawKiosk
//
//  Created by Blossom on 10-11-17.
//  Copyright 2010 FruitonLabs. All rights reserved.
//

#import "InitializationEmailSender.h"

extern NSString* kEmailSMTP ;
extern int kEmailPort ;
extern NSString* kEmailFromAddress ;
extern NSString* kEmailFromName ;
extern NSString* kEmailFromPassword ;
extern NSString* kStartupEmailToAddress ;
extern NSString* kStartupEmailSubject ;
extern NSString* kStartupEmailMessage ;


@implementation InitializationEmailSender

/*
-(id) init {
	self = [super init];
	
	[self setSenderInformationSMTP: @"smtp.gmail.com" 
							  port:587
						  fromName: @"Jocelyn Smith" 
				  fromEmailAddress: @"JigsawKiosk@gmail.com"
					  fromPassword: @"fireworks"];
	
	[self setStartupEmailSubject: @"JigsawKiosk Started Up"];
	
	return self;
}
*/

/* -----------------------------------------------
 Methods
 -------------------------------------------------*/

- (void)sendEmailMessageTo: (NSString *) recipientName AtEmail: (NSString *) recipientEmail {
	
	dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
	
	dispatch_async(queue, ^{
		
		@try {
			//Create the email agent associated with the smtp server that will be used to send emails
			emailAgent = [EDMailAgent  mailAgentForRelayHostWithName:kEmailSMTP port:kEmailPort];
			[emailAgent setUsesSecureConnections:YES]; 
			
			//Set login information
			NSMutableDictionary *authenticationInformation = [NSMutableDictionary dictionary];
			[authenticationInformation setObject:kEmailFromAddress forKey:EDSMTPUserName];
			[authenticationInformation setObject:kEmailFromPassword forKey:EDSMTPPassword];
			[emailAgent setAuthInfo:authenticationInformation];
			
			//Create default message including header
			//startupEmailMessage = kStartupEmailMessage;
			headerFields = [NSMutableDictionary dictionary];
			[headerFields setObject:kStartupEmailSubject forKey:@"Subject"];
			[headerFields setObject:recipientEmail forKey:@"To"];
			[headerFields setObject:kEmailFromName forKey:@"From"];
			[emailAgent sendMailWithHeaders:headerFields andBody:kStartupEmailMessage];
		}
		@catch (NSException * e) {
			//unsent emails are not recorded
			NSLog(@"email not sent %@", e);
		}
		
	});
}

/* -----------------------------------------------
 Getter and Setter Methods
 -------------------------------------------------*/
/*
- (void) setSenderInformationSMTP: (NSString *) smtp
							 port: (int) port
						 fromName: (NSString *) name 
				 fromEmailAddress: (NSString *) emailAddress
					 fromPassword: (NSString *) password {
	
	[self setSenderSMTP: smtp];
	[self setSenderPort:port];
	[self setSenderName: name];
	[self setSenderEmailAddress: emailAddress];
	[self setSenderPassword: password];
	
}

@synthesize senderSMTP;
@synthesize senderPort;
@synthesize senderName;
@synthesize senderEmailAddress;
@synthesize senderPassword;
@synthesize startupEmailSubject;
@synthesize startupEmailMessage;
@synthesize emailAgent;
@synthesize headerFields;
*/
 
@end
